package com.user.service

import com.data.bean.Theme
import com.data.bean.ThemeClass
import com.data.util.CheckDataUtils
import com.data.util.DatabaseIdUtils
import com.data.util.FinalStringUtils
import com.data.util.ImageLookUtils
import com.user.dao.ThemeAdminDao
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.commons.CommonsMultipartFile

import java.util.Date

/**
 * FileName: ThemeAdminService
 * Author:   HingLo
 * Date:     2017/11/6 11:41
 * Description: 主题管理的服务层
 */
@Service
class ThemeAdminService {
    @Autowired
    var themeAdminDao: ThemeAdminDao? = null

    /**
     * 根据用户的ID查询他的主题
     */
    fun listThemeAdminService(id: String): List<Theme>? {
        return if (CheckDataUtils.stringUtilsID(id)) {
            themeAdminDao!!.listThemeAdminDao(id)
        } else null
    }


    fun showImageService(imageName: String, tid: String): ByteArray? =
            ImageLookUtils.readImage(FinalStringUtils.THEME + tid + "/", imageName)


    fun submitThemeAdminService(image: CommonsMultipartFile, bImage: CommonsMultipartFile, theme: Theme): Boolean {
        val isuffix = ImageLookUtils.getFileSuffix(image)
        val bsuffix = ImageLookUtils.getFileSuffix(bImage)
        if (CheckDataUtils.iconUtils(isuffix!!) && CheckDataUtils.iconUtils(bsuffix!!)) {
            theme.id = DatabaseIdUtils.dataIdOne//主题的Id
            var imageName = DatabaseIdUtils.dataIdOne + isuffix
            theme.image = imageName
            if (ImageLookUtils.saveFile(FinalStringUtils.THEME + theme.id + "/" + imageName, image)) {//保存前景图
                imageName = DatabaseIdUtils.dataIdOne + bsuffix
                if (ImageLookUtils.saveFile(FinalStringUtils.THEME + theme.id + "/" + imageName, bImage)) { //保存背景图
                    //保存到数据库中
                    theme.bimage = imageName
                    theme.date = Date()
                    theme.isState = false//默认状态未为上线
                    theme.status = 0//默认状态未提交审核
                    return this.themeAdminDao!!.saveMassageDao(theme) > 0
                }
            }
        }
        return false
    }

    /**
     * 提交审核
     */
    fun changeThemeAdminService(id: String, state: Int): Boolean {
        if (state == 0 || state == -1 || state == -2) {
            return if (CheckDataUtils.stringUtilsID(id)) themeAdminDao!!.changeThemeAdminDao(id) > 0 else false
        }
        return false
    }

    /**
     * 删除主题
     */
    fun deleteThemeAdminService(id: String): Boolean {
        val bool = if (CheckDataUtils.stringUtilsID(id)) themeAdminDao!!.deleteThemeAdminDao(id) > 0 else false
        if (bool) ImageLookUtils.deleteDir(FinalStringUtils.THEME + id)//删除主题目录
        return bool
    }

    fun changeThemeSoftService(id: String): Boolean =
            if (CheckDataUtils.stringUtils(id)) themeAdminDao!!.changeThemeSoftDao(id) > 0 else false

    /**
     * 修改主题基本信息
     */
    fun changeMassageThemeAdminService(id: String, type: Int, value: String): Boolean = when (type) {
        1, 3, 4 -> themeAdminDao!!.changeMassageThemeAdminDao(id, value, type.toString()) > 0
        else -> false
    }

    fun changeMassageImageThemeAdminService(id: String, type: Int, image: CommonsMultipartFile): Boolean {
        val isuffix = ImageLookUtils.getFileSuffix(image)
        if (CheckDataUtils.iconUtils(isuffix!!)) {
            val imageName = DatabaseIdUtils.dataIdOne + isuffix


            if (ImageLookUtils.saveFile(FinalStringUtils.THEME + id + "/" + imageName, image)) {
                return when (type) {
                    2, 5 -> themeAdminDao!!.changeMassageThemeAdminDao(id, imageName, type.toString()) > 0
                    else -> false
                }

            }


        }
        return false
    }

    fun tipErrorService(id: String): String = themeAdminDao!!.tipErrorDao(id)

}
